
package Engine.Game;

import Audio.MusicPlayer;
import Engine.Character.OverworldCharacter;
import Engine.Scene.BattleManager;
import Engine.Scene.Map;
import java.io.IOException;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.LayerManager;

/**
 * 
 * @author Gerardo Galíndez
 */
public class GameAdmin extends LayerManager {

    //------------------------------------------------------------------
    //Objects of game---------------------------------------------------
    private Map transitableMap;
    private Map boundsMap;
    private OverworldCharacter mainPlayer;
    private OverworldCharacter secondPlayer;
    private BattleManager battleManager;
    private GameDisplay currentDisplay;
    private MusicPlayer music;
    private char difficulty;
    //Character information---------------------------------------------
    private int xPosition;
    private int yPosition;
    //Coordinates of map scrolling--------------------------------------
    private int movementX = 0;
    private int movementY = 0;
    //Integer to show the current scene on display----------------------
    private int currentScene;
    //Number of steps to start a battle---------------------------------
    private int stepsToNextBattle;
    //Character constants-----------------------------------------------
    private int playerWidth = 20;
    private int playerHeight = 2;
    //Display and map constants-----------------------------------------
    private int screenWidth, screenHeight;
    private int HORIZONTAL_LEFT_MAP_MOVEMENT_ANCHOR;
    private int VERTICAL_TOP_MAP_MOVEMENT_ANCHOR;
    private int HORIZONTAL_RIGHT_MAP_MOVEMENT_ANCHOR;
    private int VERTICAL_DOWN_MAP_MOVEMENT_ANCHOR;
    private static final int TILE_SIZE = 32;
    private static final int HORIZONTAL_TILES = 15;
    private static final int VERTICAL_TILES = 10;
    private static final int MAP_WIDTH = HORIZONTAL_TILES * TILE_SIZE;
    private static final int MAP_HEIGHT = VERTICAL_TILES * TILE_SIZE;
    //Character directions constants------------------------------------
    /**
     * Integer index code if character is not moving
     */
    public static final int DIRECTION_STAND = 0;
    /**
     * Integer index code if character is moving upwards
     */
    public static final int DIRECTION_UP = 1;
    /**
     * Integer index code if character is moving downwards
     */
    public static final int DIRECTION_DOWN = 2;
    /**
     * Integer index code if character is moving to the left
     */
    public static final int DIRECTION_LEFT = 3;
    /**
     * Integer index code if character is moving to the right
     */
    public static final int DIRECTION_RIGHT = 4;
    //------------------------------------------------------------------

    /**
     * Creates a new object
     * @param screenWidth of the screen
     * @param screenHeight of the screen
     * @param character main player of game
     * @param currentDisplay 
     */
    public GameAdmin(int screenWidth, int screenHeight, int character, GameDisplay currentDisplay, boolean isNewGame, String characterData, char difficulty) {
        try {
            //Loading game constants
            this.screenWidth = screenWidth;
            this.screenHeight = screenHeight;
            this.currentDisplay = currentDisplay;
            this.difficulty = difficulty;
            this.HORIZONTAL_LEFT_MAP_MOVEMENT_ANCHOR = screenWidth / 2;
            this.VERTICAL_TOP_MAP_MOVEMENT_ANCHOR = screenHeight / 2;
            this.HORIZONTAL_RIGHT_MAP_MOVEMENT_ANCHOR = MAP_WIDTH - screenWidth / 2;
            this.VERTICAL_DOWN_MAP_MOVEMENT_ANCHOR = MAP_HEIGHT - screenHeight / 2;
            if ( isNewGame ) {
                currentScene = Map.FISHBOWL;
                this.loadMainPlayer(character);
                mainPlayer.setPosition(30, 50);
            } else {
                this.loadData(characterData);
            }
            this.append(mainPlayer);
            this.loadMusic();
            this.loadMap();
            this.battleManager = new BattleManager(this, mainPlayer, currentDisplay);
            //Finish loading game
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

//<<--------------------------------------------------Loading elements------------------------------------------------------------------->>

    /**
     * Start a game with the data saved in a file
     * @param characterData the data saved
     * @throws IOException If an image doesn't exists
     */
    public void loadData(String characterData) throws IOException{
        int firstMarker, secondMarker, thirdMarker, fourthMarker, fifthMarker, sixthMarker, seventhMarker, player;
        String hp, mp, xp, level;
        this.currentScene = Integer.parseInt(characterData.substring(0,1));
        player = Integer.parseInt(characterData.substring(2, 3));
        this.loadMainPlayer(player);
        firstMarker = characterData.indexOf('.');
        secondMarker = characterData.indexOf('.', firstMarker+1);
        thirdMarker = characterData.indexOf('.', secondMarker+1);
        fourthMarker = characterData.indexOf('.', thirdMarker+1);
        fifthMarker = characterData.indexOf('.', fourthMarker+1);
        sixthMarker = characterData.indexOf('.', fifthMarker+1);
        seventhMarker = characterData.indexOf('.', sixthMarker+1);
        hp = characterData.substring(secondMarker+1, thirdMarker);
        mp = characterData.substring(thirdMarker+1, fourthMarker);
        xp = characterData.substring(fourthMarker+1, fifthMarker);
        level = characterData.substring(fifthMarker+1, sixthMarker);
        this.xPosition = Integer.parseInt(characterData.substring(sixthMarker+1, seventhMarker));
        this.yPosition = Integer.parseInt(characterData.substring(seventhMarker+1));
        this.mainPlayer.setMaxHealthPoints(Integer.parseInt(hp));
        this.mainPlayer.setMaxMagicPoints(Integer.parseInt(mp));
        this.mainPlayer.addExperiencePoints(Integer.parseInt(xp));
        this.mainPlayer.setCurrentLevel(Integer.parseInt(level));
        this.mainPlayer.setPosition(xPosition, yPosition);
    }

    /**
     * Loads the music object corresponding to currentScene. The method tests if the object has been created or not. If it's already created,
     * it calls the method changeTrack() at MusicPlayer to change the Player object and load a new audio file. If it hasn't been created, it
     * creates a new object with its corresponding reference.
     * @see changeTrack()
     */
    public void loadMusic() {
        if(music != null) {
            music.changeTrack(currentScene);
        } else {
            music = new MusicPlayer(currentScene);
        }
    }

    public void resumeMusic() {
        music.resume();
    }

    public void toggleMusic() {
        music.toggleMusic();
    }
    
    /**
     * Loads the corresponding map to the current scene. The variables are checked, if they are already created, the LayerManager removes them.
     * If not, the method proceeds normally to the next map creation. Using the variable currentScene, the method decides which type of Map
     * subclass should be created. Two objects are created, a map with the transitable information and another one with the bounds information.
     * Once finished, the LayerManager appends them in the same order, boundsMap first (upper) and transitableMap last (lower)
     * @throws IOException Image path error
     */
    public void loadMap() throws IOException {
        if (boundsMap != null && transitableMap != null) {
            this.remove(boundsMap);
            this.remove(transitableMap);
        }
        switch (currentScene) {
            case Map.FISHBOWL:
                transitableMap = new Map(Map.FISHBOWL, false, this);
                boundsMap = new Map(Map.FISHBOWL, true, this);
                currentDisplay.setMiniMap(Image.createImage("/Images/Maps/Fishbowl-Minimap.png"));
                break;
            case Map.OUTER_FISHBOWL:
                transitableMap = new Map(Map.OUTER_FISHBOWL, false, this);
                boundsMap = new Map(Map.OUTER_FISHBOWL, true, this);
                currentDisplay.setMiniMap(Image.createImage("/Images/Maps/OuterFishbowl-Minimap.png"));
                break;
            case Map.DISTILLERY:
                transitableMap = new Map(Map.DISTILLERY, false, this);
                boundsMap = new Map(Map.DISTILLERY, true, this);
                currentDisplay.setMiniMap(Image.createImage("/Images/Maps/Distillery-Minimap.png"));
                break;
            case Map.OUTER_DISTILLERY:
                transitableMap = new Map(Map.OUTER_DISTILLERY, false, this);
                boundsMap = new Map(Map.OUTER_DISTILLERY, true, this);
                currentDisplay.setMiniMap(Image.createImage("/Images/Maps/OuterDistillery-Minimap.png"));
                break;
            case Map.CCI_RECEIVER:
                transitableMap = new Map(Map.CCI_RECEIVER, false, this);
                boundsMap = new Map(Map.CCI_RECEIVER, true, this);
                currentDisplay.setMiniMap(Image.createImage("/Images/Maps/CCI-Receiver-Minimap.png"));
                break;
            case Map.CCI_COMPUTERS:
                transitableMap = new Map(Map.CCI_COMPUTERS, false, this);
                boundsMap = new Map(Map.CCI_COMPUTERS, true, this);
                currentDisplay.setMiniMap(Image.createImage("/Images/Maps/CCI-Computers-Minimap.png"));
                break;
            case Map.WOLVES_UPPER_LEFT:
                transitableMap = new Map(Map.WOLVES_UPPER_LEFT, false, this);
                boundsMap = new Map(Map.WOLVES_UPPER_LEFT, true, this);
                currentDisplay.setMiniMap(Image.createImage("/Images/Maps/Wolves-Minimap.png"));
                break;
            case Map.WOLVES_UPPER_RIGHT:
                transitableMap = new Map(Map.WOLVES_UPPER_RIGHT, false, this);
                boundsMap = new Map(Map.WOLVES_UPPER_RIGHT, true, this);
                currentDisplay.setMiniMap(Image.createImage("/Images/Maps/Wolves-Minimap.png"));
                break;
            case Map.WOLVES_LOWER_LEFT:
                transitableMap = new Map(Map.WOLVES_LOWER_LEFT, false, this);
                boundsMap = new Map(Map.WOLVES_LOWER_LEFT, true, this);
                currentDisplay.setMiniMap(Image.createImage("/Images/Maps/Wolves-Minimap.png"));
                break;
            case Map.WOLVES_LOWER_RIGHT:
                transitableMap = new Map(Map.WOLVES_LOWER_RIGHT, false, this);
                boundsMap = new Map(Map.WOLVES_LOWER_RIGHT, true, this);
                currentDisplay.setMiniMap(Image.createImage("/Images/Maps/Wolves-Minimap.png"));
                break;
            case Map.OUTSIDES:
                transitableMap = new Map(Map.OUTSIDES, false, this);
                boundsMap = new Map(Map.OUTSIDES, true, this);
                currentDisplay.setMiniMap(Image.createImage("/Images/Maps/Outsides-Minimap.png"));
                break;
            default:
                System.out.println("Image error.");
        }
        this.append(boundsMap);
        this.append(transitableMap);
    }
    
    /**
     * Selects the attributes of the main player, established at OverworldCharacter. The integer character is passed down from the MIDlet.
     * @param character OverworldCharacter integer index code representing a user character.
     */
    public void loadMainPlayer(int character) throws IOException {
        //Character constructor must be modified to receive an image.
        mainPlayer = new OverworldCharacter(character, this, this.difficulty);
    }

    /**
     * This method checks where the player is and check which gate is near.
     * Depending of the gate, GameAdmin sets the player position in the correct place.
     * @param scene current scene
     * @param gate near main player
     */
    public void setPlayerPosition(int scene, int gate){
        switch (scene) {
            case Map.FISHBOWL:
                switch (gate) {
                    case Map.WOLVES_LOWER_LEFT:
                        mainPlayer.setPosition(xPosition, MAP_HEIGHT - (TILE_SIZE * 2));
                        break;
                    case Map.OUTER_FISHBOWL:
                        mainPlayer.setPosition(xPosition, Map.TILE_SIZE * 2);
                        break;
                    case Map.DISTILLERY:
                        mainPlayer.setPosition(Map.TILE_SIZE, MAP_HEIGHT - (TILE_SIZE * 2));
                        break;
                    default:
                        System.out.println("Bad exit");
                }
                break;
            case Map.OUTER_FISHBOWL:
                switch (gate) {
                    case Map.FISHBOWL:
                        mainPlayer.setPosition(TILE_SIZE, MAP_HEIGHT - (TILE_SIZE * 2));
                        break;
                    case Map.OUTER_DISTILLERY:
                        mainPlayer.setPosition(Map.TILE_SIZE*2, yPosition);
                        break;
                    default:
                        System.out.println("UNKNOWN GATE");
                }
                break;
            case Map.DISTILLERY:
                switch (gate) {
                    case Map.FISHBOWL:
                        mainPlayer.setPosition(MAP_WIDTH - (TILE_SIZE * 2), MAP_HEIGHT - (TILE_SIZE * 2));
                        break;
                    case Map.OUTER_DISTILLERY:
                        mainPlayer.setPosition(xPosition, Map.TILE_SIZE*2);
                        break;
                    case Map.WOLVES_LOWER_RIGHT:
                        mainPlayer.setPosition(xPosition, Map.MAP_HEIGHT - (Map.TILE_SIZE * 2));
                        break;
                    default:
                        System.out.println("UNKNOWN GATE");
                }
                break;
            case Map.OUTER_DISTILLERY:
                switch (gate) {
                    case Map.DISTILLERY:
                        mainPlayer.setPosition(xPosition, Map.MAP_HEIGHT-(Map.TILE_SIZE*2));
                        break;
                    case Map.OUTER_FISHBOWL:
                        mainPlayer.setPosition(Map.MAP_WIDTH-(Map.TILE_SIZE*2), yPosition);
                        break;
                    case Map.OUTSIDES:
                        mainPlayer.setPosition(xPosition, Map.TILE_SIZE*2);
                        break;
                }
                break;
            case Map.WOLVES_LOWER_LEFT:
                switch (gate) {
                    case Map.FISHBOWL:
                        mainPlayer.setPosition(xPosition, Map.TILE_SIZE * 2);
                        break;
                    case Map.WOLVES_LOWER_RIGHT:
                        mainPlayer.setPosition(Map.TILE_SIZE * 2, yPosition);
                        break;
                    case Map.WOLVES_UPPER_LEFT:
                        mainPlayer.setPosition(xPosition, Map.MAP_HEIGHT - (Map.TILE_SIZE * 2));
                        break;
                }
                break;
            case Map.WOLVES_UPPER_LEFT:
                switch (gate) {
                    case Map.WOLVES_UPPER_RIGHT:
                        mainPlayer.setPosition(Map.TILE_SIZE * 2, yPosition);
                        break;
                    case Map.WOLVES_LOWER_LEFT:
                        mainPlayer.setPosition(xPosition, Map.TILE_SIZE * 2);
                        break;
                }
                break;
            case Map.WOLVES_LOWER_RIGHT:
                switch (gate) {
                    case Map.WOLVES_LOWER_LEFT:
                        mainPlayer.setPosition(Map.MAP_WIDTH - (Map.TILE_SIZE * 2), yPosition);
                        break;
                    case Map.WOLVES_UPPER_RIGHT:
                        mainPlayer.setPosition(xPosition, Map.MAP_HEIGHT - (Map.TILE_SIZE * 2));
                        break;
                    case Map.DISTILLERY:
                        mainPlayer.setPosition(xPosition, Map.TILE_SIZE * 2);
                        break;
                }
                break;
            case Map.WOLVES_UPPER_RIGHT:
                switch (gate) {
                    case Map.WOLVES_UPPER_LEFT:
                        mainPlayer.setPosition(Map.MAP_WIDTH - (Map.TILE_SIZE * 2), yPosition);
                        break;
                    case Map.WOLVES_LOWER_RIGHT:
                        mainPlayer.setPosition(xPosition, Map.TILE_SIZE * 2);
                        break;
                    case Map.CCI_RECEIVER:
                        mainPlayer.setPosition(Map.TILE_SIZE * 4, Map.MAP_HEIGHT - Map.TILE_SIZE);
                        break;
                    case Map.CCI_RECEIVER_2:
                        mainPlayer.setPosition(Map.TILE_SIZE * 10, Map.MAP_HEIGHT - Map.TILE_SIZE);
                        break;
                }
                break;
            case Map.CCI_COMPUTERS:
                switch (gate) {
                    case Map.CCI_RECEIVER:
                        mainPlayer.setPosition(Map.TILE_SIZE * 2, yPosition - Map.TILE_SIZE);
                        break;
                }
                break;
            case Map.CCI_RECEIVER:
                switch (gate) {
                    case Map.CCI_COMPUTERS:
                        mainPlayer.setPosition(Map.MAP_WIDTH - (Map.TILE_SIZE * 2), yPosition + Map.TILE_SIZE);
                        break;
                    case Map.WOLVES_UPPER_RIGHT:
                        mainPlayer.setPosition(Map.MAP_WIDTH - (Map.TILE_SIZE * 2), 0);
                        break;
                    case Map.WOLVES_UPPER_RIGHT_2:
                        mainPlayer.setPosition(Map.MAP_WIDTH - (Map.TILE_SIZE * 2), Map.TILE_SIZE * 4);
                        break;
                }
            case Map.OUTSIDES:
                switch (gate ) {
                    case Map.OUTER_DISTILLERY:
                        mainPlayer.setPosition(xPosition, Map.MAP_HEIGHT-(Map.TILE_SIZE*2));
                        break;
                }
        }
    }

    /**
     * This method changes the current scene.
     * @param nextScene to be painted
     */
    public void setCurrentScene(int nextScene){
        this.currentScene = nextScene;
    }
//<<------------------------------------------------------------------------------------------------------------------------------------------------->>
//<<------------------------------------------------Movement and interactions------------------------------------------------------------------------>>

    /**
     * This method check for the position of the character. If character is right of the center of
     * the display, GameAdmin paints the map with the difference between the x position of the character
     * and half the screen width in the map x position and y position equals to cero.
     * If character is down the center, GameAdmin paints the map with the difference between the y position of the character
     * and half the screen height in the map y position and x position equals to cero.
     */
    public void mapMovement() {
        if (mainPlayer.getX() > this.HORIZONTAL_LEFT_MAP_MOVEMENT_ANCHOR) {
            if(mainPlayer.getX() < this.HORIZONTAL_RIGHT_MAP_MOVEMENT_ANCHOR) {
                movementX = mainPlayer.getX() - screenWidth / 2;
            } else {
                movementX = this.HORIZONTAL_RIGHT_MAP_MOVEMENT_ANCHOR - screenWidth / 2;
            }
        } else {
            movementX = 0;
        }
        if (mainPlayer.getY() > VERTICAL_TOP_MAP_MOVEMENT_ANCHOR) {
            if(mainPlayer.getY() < VERTICAL_DOWN_MAP_MOVEMENT_ANCHOR) {
                movementY = mainPlayer.getY() - screenHeight / 2;
            } else {
                movementY = this.VERTICAL_DOWN_MAP_MOVEMENT_ANCHOR - screenHeight / 2;
            }
        } else {
            movementY = 0;
        }
        this.setViewWindow(movementX, movementY, screenWidth, screenHeight);
    }

    /**
     * The method checks for the position of the mainPlayer and then checks the direction.
     * Whatever direction it is, GameAdmin calls the sprite class to move the character and
     * then checks for collision with the objects of the map and the perimeter.
     * At last, this method check if map has to be changed.
     * @param direction the direction the character is running
     */
    public void movePlayer(int direction) {
        xPosition = mainPlayer.getX();
        yPosition = mainPlayer.getY();

        if (direction == DIRECTION_STAND) {
            mainPlayer.stand();
        }
        if (direction == DIRECTION_DOWN) {
            if (mainPlayer.getY() < MAP_HEIGHT - playerHeight) {
                mainPlayer.moveDown();
                if (this.checkPlayerCollision()) {
                    mainPlayer.setPosition(xPosition, yPosition - 2);
                }
            } else {
                this.changeMap(xPosition, yPosition);
            }
        } else if (direction == DIRECTION_UP) {
            if (mainPlayer.getY() >= 8) {
                mainPlayer.moveUp();
                if (this.checkPlayerCollision()) {
                    mainPlayer.setPosition(xPosition, yPosition + 2);
                }
            } else {
                this.changeMap(xPosition, yPosition);
            }
        } else if (direction == DIRECTION_LEFT) {
            if (mainPlayer.getX() >= 0) {
                mainPlayer.moveLeft();
                if (this.checkPlayerCollision()) {
                    mainPlayer.setPosition(xPosition + 2, yPosition);
                }
            } else {
                this.changeMap(xPosition, yPosition);
            }
        } else if (direction == DIRECTION_RIGHT) {
            if (mainPlayer.getX() <= MAP_WIDTH - playerWidth) {
                mainPlayer.moveRight();
                if (this.checkPlayerCollision()) {
                    mainPlayer.setPosition(xPosition - 2, yPosition);
                }
            } else {
                this.changeMap(xPosition, yPosition);
            }
        }
        //this.changeMap(xPosition, yPosition);
    }

    /**
     * Checks if map need to be changed. If yes, it calls the respective map to check
     * the bounds and then load the corresponding map.
     *
     * @param xPosition Character position on map along the horizontal line
     * @param yPosition Character position on map along the vertical line
     */
    public void changeMap(int xPosition, int yPosition) {
        switch (currentScene) {
            case Map.FISHBOWL:
                transitableMap.checkFishBowlBounds(xPosition, yPosition);
                if (currentScene != Map.FISHBOWL) {
                    this.load();
                }
                break;
            case Map.OUTER_FISHBOWL:
                transitableMap.checkOuterFishBowlBounds(xPosition, yPosition);
                if (currentScene != Map.OUTER_FISHBOWL) {
                    this.load();
                }
                break;
            case Map.DISTILLERY:
                transitableMap.checkDistilleryBounds(xPosition, yPosition);
                if(currentScene != Map.DISTILLERY){
                    this.load();
                }
                break;
            case Map.CCI_RECEIVER:
                transitableMap.checkCCIReceiverBounds(xPosition, yPosition);
                if ( currentScene != Map.CCI_RECEIVER ) {
                    this.load();
                }
                break;
            case Map.CCI_COMPUTERS:
                transitableMap.checkCCIComputersBounds(xPosition, yPosition);
                if ( currentScene != Map.CCI_COMPUTERS ) {
                    this.load();
                }
                break;
            case Map.WOLVES_LOWER_LEFT:
                transitableMap.checkWolves3Bounds(xPosition, yPosition);
                if ( currentScene != Map.WOLVES_LOWER_LEFT ) {
                    this.load();
                }
                break;
            case Map.WOLVES_LOWER_RIGHT:
                transitableMap.checkWolves4Bounds(xPosition, yPosition);
                if ( currentScene != Map.WOLVES_LOWER_RIGHT ) {
                    this.load();
                }
                break;
            case Map.WOLVES_UPPER_LEFT:
                transitableMap.checkWolves1Bounds(xPosition, yPosition);
                if ( currentScene != Map.WOLVES_UPPER_LEFT ) {
                    this.load();
                }
            break;
            case Map.WOLVES_UPPER_RIGHT:
                transitableMap.checkWolves2Bounds(xPosition, yPosition);
                if ( currentScene != Map.WOLVES_UPPER_RIGHT ) {
                    this.load();
                }
                break;
            case Map.OUTER_DISTILLERY:
                transitableMap.checkOuterDistilleryBounds(xPosition, yPosition);
                if ( currentScene != Map.OUTER_DISTILLERY ) {
                    this.load();
                }
                break;
            case Map.OUTSIDES:
                transitableMap.checkOutsidesBounds(xPosition, yPosition);
                if ( currentScene != Map.OUTSIDES ) {
                    this.load();
                }
                break;
            default:
                System.out.println("UNKNOWN SCENE");
        }
    }

    /**
     * Loads the corresponding music and map depending of
     * current scene.
     */
    public void load() {
        try {
            this.loadMap();
            this.loadMusic();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    /**
     * Checks for collision between a sprite and the boundary map.
     * @return a boolean value if a collision has been ocurred
     */
    public boolean checkPlayerCollision() {
        return mainPlayer.collidesWith(boundsMap, true);
    }

//<<--------------------------------------------------------------------------------------------------------------------->>
//<<--------------------------------------------------Battle management-------------------------------------------------->>

    /**
     * Notify the Battle Manager that a battle has to start.
     * @param battleMusicCode index code to select the music depending of the current scene
     */
    public void startBattle(int battleMusicCode) {
        currentDisplay.startBattle();
        music.startBattleMusic(battleMusicCode);
    }

    /**
     * Loads the music of overworld screen
     */
    public void finishBattle() {
        //System.out.println("Battle finished. Steps to next battle: " + stepsToNextBattle);
        this.loadMusic();
    }

    /**
     * Check if a battle is coming. If stepsToNextBattle equals zero, a battle is triggered and notifies the BattleManager and GameDisplay.
     * If not it reduces by one the stepsToNextBattle variable..
     */
    public void step() {
        if (stepsToNextBattle == 0) {
            battleManager.startBattle();
        } else {
            stepsToNextBattle--;
            //System.out.println(stepsToNextBattle);
        }
    }

    /**
     * This method sets the steps to trigger the next battle.
     * @param steps needed to next battle
     */
    public void setNextBattle(int steps) {
        this.stepsToNextBattle = steps;
    }

//<<------------------------------------------------------------------------------------------------------------------>>
//<<--------------------------------------------------------Accesor methods------------------------------------------->>

    /**
     * Returns the current character on display
     * @return mainPlayer
     */
    public OverworldCharacter getPlayer(){
        return this.mainPlayer;
    }

    /**
     * Returns the current scene on display
     * @return currentScene
     */
    public int getCurrentScene() {
        return currentScene;
    }

    public void shutDownMusic(){
        this.music.shutDown();
        this.music = null;
    }
//<<-------------------------------------------------------------------------------------------------------->>
//<<--------------------------------------------------------Painting---------------------------------------->>

    /**
     * This method check if current map needs to scroll. Then it calls
     * LayerManager to paint the area beginning at (0,0).
     * @param g for painting the game canvas
     */
    public void paint(Graphics g) {
        this.mapMovement();
        super.paint(g, 0, 0);
    }

//<<-------------------------------------------------------------------------------------------------------->>
}